package com.xgimi.open.skill.datamanager.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgimi.open.skill.datamanager.domain.entity.ProductDo;
import com.xgimi.open.skill.datamanager.domain.mapper.ProductMapper;
import com.xgimi.open.skill.datamanager.domain.service.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author coderzpw.zhang
 * @description 针对表【product(产品表)】的数据库操作Service实现
 * @createDate 2023-02-05 17:44:37
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDo>
        implements ProductService {

    @Override
    public Boolean saveProduct(ProductDo productDo) {
        return this.save(productDo);
    }

    @Override
    public List<ProductDo> queryProductByName(ProductDo productDo) {
        LambdaQueryWrapper<ProductDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(productDo.getProductName()), ProductDo::getProductName, productDo.getProductName());
        List<ProductDo> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public IPage<ProductDo> pageQuery(IPage<ProductDo> page, ProductDo mapperReq) {
        LambdaQueryWrapper<ProductDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(mapperReq.getProductStatus()), ProductDo::getProductStatus, mapperReq.getProductStatus());
        queryWrapper.eq(!Objects.isNull(mapperReq.getOperatingSystem()), ProductDo::getOperatingSystem, mapperReq.getOperatingSystem());
        queryWrapper.eq(!Objects.isNull(mapperReq.getType()), ProductDo::getType, mapperReq.getType());
        queryWrapper.eq(!Objects.isNull(mapperReq.getCreatedBy()), ProductDo::getCreatedBy, mapperReq.getCreatedBy());
        if (!Objects.isNull(mapperReq.getId()) || !StringUtils.isEmpty(mapperReq.getProductName())) {
            queryWrapper.and(wapper -> {
                wapper.eq(!Objects.isNull(mapperReq.getId()), ProductDo::getId, mapperReq.getId())
                        .or()
                        .eq(!StringUtils.isEmpty(mapperReq.getProductName()), ProductDo::getProductName, mapperReq.getProductName());
            });
        }
        queryWrapper.orderByDesc(ProductDo::getId);
        return this.page(page, queryWrapper);
    }

    @Override
    public ProductDo queryById(Long id) {
        return this.getById(id);
    }

    @Transactional
    @Override
    public Boolean saveTestP(ProductDo productDo) {
        productDo.setProductName("xxx4");
        boolean save = this.save(productDo);
        System.out.println("新增的id：" + productDo.getId());
        return true;
    }
}




